<?php

namespace App\Http\Controllers\Admin;

use App\FriendlyLink as FriendlyLinkModel;
use \Request;

class FriendlyLink extends Base
{
    public function index()
    {
        $friendlyLinkList = FriendlyLinkModel::orderBy('id', 'desc')->paginate(15);

        $count = FriendlyLinkModel::count();
        return view('admin.friendly_link.index', [
            'count'            => $count,
            'friendlyLinkList' => $friendlyLinkList,
        ]);
    }

    public function create()
    {
        return view('admin.friendly_link.create');
    }

    public function store()
    {
        $result = FriendlyLinkModel::create([
            'title'   => Request::input('title', ''),
            'sort'    => Request::input('sort', 0),
            'is_show' => Request::input('is_show', 0),
            'url'     => Request::input('url', ''),
        ]);
        return $result ? showSuccess('添加友情链接成功！') : showError('添加友情链接失败！');
    }

    public function edit($id = '')
    {
        $friendlyInfo = FriendlyLinkModel::find($id);
        return view('admin.friendly_link.edit', ['info' => $friendlyInfo]);
    }

    public function update($id = '')
    {
        $result = FriendlyLinkModel::where('id', '=', $id)->update([
            'title'   => Request::input('title', ''),
            'sort'    => Request::input('sort', 0),
            'is_show' => Request::input('is_show', 0),
            'url'     => Request::input('url', ''),
        ]);
        return $result ? showSuccess('修改友情链接成功！') : showError('修改友情链接失败！');
    }

    public function destroy($id = '')
    {
        $result = FriendlyLinkModel::where('id', '=', $id)->delete();
        return $result ? showSuccess('删除友情链接成功！') : showError('删除友情链接失败！');

    }
}
